setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top1domText <- read.csv("textpool/top1domText.csv", stringsAsFactors = FALSE)

OLnarrowcareful <- validateLabel(type = "OL", n = 500, 
                                text.predict = top1domText, text.name = "post_text",
                                labels = c("Equal Pay for Women",
                                           "Healthcare/Reproductive Rights",
                                           "Agriculture",
                                           "Student Loan/Debt",
                                           "Drug Abuse",
                                           "Higher Education/Job Training",
                                           "Wall Street/Financial Sector",
                                           "Government Sutdown/Congressional Budget",
                                           "Obamacare/Tax Policy",
                                           "Deficits/Debt/Budget"), 
                                labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                labels.add = NULL)

goldOL <- read.csv("goldstandard/goldOL.csv", stringsAsFactors = FALSE)
OLnarrowcareful <- mixGold(tasks = OLnarrowcareful, golds = goldOL)
OLnarrowcareful <- recordTasks(type = "OL", tasks = OLnarrowcareful,
                              path = "labelrecords/OLnarrowcareful.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# OLnarrowcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLnarrowcareful, HITidspath = "HITids/OLnarrowcarefulids1.Rdata")
# ### extend HITs if needed
# for(i in OLnarrowcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLnarrowcarefulids1.Rdata")
# OLnarrowcarefulresults1 <- getResults(batch_id = "OLnarrowcareful1", hit_ids = HITids, retry = FALSE)
# write.csv(OLnarrowcarefulresults1, "OL/results/OLnarrowcarefulresults1.csv", row.names = FALSE)
# evalResults(results = OLnarrowcarefulresults1, key = OLnarrowcareful, type = "OL")
# 
# ### send tasks (second trial)
# OLnarrowcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLnarrowcareful, HITidspath = "HITids/OLnarrowcarefulids2.Rdata")
# ### extend HITs if needed
# for(i in OLnarrowcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLnarrowcarefulids2.Rdata")
# OLnarrowcarefulresults2 <- getResults(batch_id = "OLnarrowcareful2", hit_ids = HITids, retry = FALSE)
# write.csv(OLnarrowcarefulresults2, "OL/results/OLnarrowcarefulresults2.csv", row.names = FALSE)
# evalResults(results = OLnarrowcarefulresults2, key = OLnarrowcareful, type = "OL")